import 'package:flutter/material.dart';
import 'package:hello/old/day10Route/about.dart';
import 'package:hello/old/day10Route/detail.dart';
import 'package:hello/old/day10Route/unknown.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: HomePage(),
      initialRoute: HomePage.routeName,
      routes: {
        HomePage.routeName:(ctx) => HomePage(),
        AboutScreen.routeName:(ctx) => AboutScreen()
      },
      onUnknownRoute: (settings){
        return MaterialPageRoute(builder: (ctx){
          return UnKnownScreen();
        });
      },
    );
  }
}

class HomePage extends StatelessWidget {
  static const routeName = "/";
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text("route"),
          centerTitle: true,
      ),
      body: Content(),
    );
  }
}

class Content extends StatefulWidget {
  const Content({Key key}) : super(key: key);

  @override
  _ContentState createState() => _ContentState();
}

class _ContentState extends State<Content> {
  String _homeMessage = "default message";

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(_homeMessage),
          ElevatedButton(
            child: Text("跳转到详情"),
            onPressed: () => _jumpToDeatil(context),
          ),
          ElevatedButton(
            child: Text("跳转到关于"),
            onPressed: () => _jumpToAbout(context),
          ),
          ElevatedButton(
            child: Text("跳转到设置"),
            onPressed: () => _jumpToSettings(context),
          ),
        ],
      ),
    );
  }

  _jumpToDeatil(BuildContext context){
    Future res =  Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
      //通过构造方法传递参数
      return Detail("home message");
    }));
    res.then((value){
      setState(() {
        _homeMessage = value;
      });
    });
  }
  _jumpToAbout(BuildContext context){
    Future res = Navigator.of(context).pushNamed(AboutScreen.routeName,arguments: "home to about message");
    res.then((value){
      setState(() {
        _homeMessage = value;
      });
    });
  }
  _jumpToSettings(BuildContext context){
    Navigator.of(context).pushNamed("/settings");
  }
}